﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using com.huawei.esdk.tp.professional.DataType;

namespace eSDK_TP_Demo_CS_CSharp.FunctionPage
{
    public partial class ConnectSites_UI : UserControl
    {
        ConferenceService service = new ConferenceService();
        AuthorizeService loginService = new AuthorizeService();
        QueryResultCode queryResultCode = new QueryResultCode();
        private System.Timers.Timer timer = null;
        delegate void textDelegate();
        bool falg = true;


        public ConnectSites_UI()
        {
            InitializeComponent();
        }

        private void ConnectSites_UI_Loaded(object sender, RoutedEventArgs e)
        {
        }

        private void ConnectSite_Click(object sender, RoutedEventArgs e)
        {
            loginService.Login();
            List<string> list = new List<string>();
            foreach (SiteStatusEx se in this.ConfSiteList.SelectedItems)
            {
                list.Add(se.uri);
            }

            int result = service.ConnectSites(this.confIDText.Text, list);
            if (result == 0)
            {
                this.OperationDefeatText.Visibility = Visibility.Collapsed;
                this.OperationSucceedText.Visibility = Visibility.Visible;
            }
            else
            {
                this.OperationDefeatText.Visibility = Visibility.Visible;
                this.OperationSucceedText.Visibility = Visibility.Collapsed;
            }
            this.ResultCodeText.Text = result.ToString();
            loginService.Logout();
        }

        private void confIDText_Changed(object sender, TextChangedEventArgs e)
        {
            if (!falg)
            {
                timer = new System.Timers.Timer();
                timer.Interval = 3000;
                timer.Elapsed += new System.Timers.ElapsedEventHandler(AliveCode);
                timer.AutoReset = false;
                timer.Enabled = true;
            }
            else
            {
                falg = false;
            }
        }

        
        private void AliveCode(object source, System.Timers.ElapsedEventArgs e)
        {
            Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new textDelegate(getConfSiteList));
        }

        private void getConfSiteList()
        {
            loginService.Login();
            queryResultCode = service.QueryConfSitesStatus(this.confIDText.Text);
            if (queryResultCode.Result != 0)
            {
                this.OperationSucceedText.Visibility = Visibility.Collapsed;
                this.OperationDefeatText.Visibility = Visibility.Visible;
                this.ResultCodeText.Text = queryResultCode.Result.ToString();
            }
            else
            {
                this.ConfSiteList.ItemsSource = queryResultCode.SiteStatusList;
                this.OperationSucceedText.Visibility = Visibility.Collapsed;
                this.OperationDefeatText.Visibility = Visibility.Collapsed;
                this.ResultCodeText.Text = "";
            }
            loginService.Logout();
            timer.Stop();
            timer.Close();
        }

        private void Page_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.Line1.X2 = this.GridWidth.ActualWidth;
            this.Line2.X2 = this.GridWidth.ActualWidth;
            this.Line3.X2 = this.GridWidth.ActualWidth;
        }
    }
}
